#include "atlas.h"
#include "pnf_mng.h"

bool read_atlas(std::ifstream& ifile, string& line, Atlas& atlas) {
	trim(line, atlas.png);
	int count = -1;

	vector<string> strs;
	Node node;
	while (!ifile.eof()) {
		getline(ifile, line);
		if (line.empty()) {
			continue;
		}
		if (line.find(".png") != string::npos) {
			return true;
		}
		switch (count) {
		case -1:
			trim(line, atlas.format);
			if (atlas.format.find("format") != string::npos) {
				count -= 2;
				break;
			}
			split(line, ":", strs);
			line = strs[1];
			split(line, ",", strs);
			atlas.width = std::atoi(strs[0].c_str());
			atlas.height = std::atoi(strs[1].c_str());
			--count;
			break;
		case -2:
			trim(line, atlas.format);
			--count;
			break;
		case -3:
			trim(line, atlas.filter);
			--count;
			break;
		case -4:
			trim(line, atlas.repeat);
			atlas.nodes.clear();
			count = 0;
			break;
		case 0:
			trim(line, node.name);
			++count;
			break;
		case 1:
			split(line, ":", strs);
			node.rotate = strs[1] == "true" || strs[1] == "TRUE";
			++count;
			break;
		case 2:
			split(line, ":", strs);
			line = strs[1];
			split(line, ",", strs);
			node.x = std::atoi(strs[0].c_str());
			node.y = std::atoi(strs[1].c_str());
			++count;
			break;
		case 3:
			split(line, ":", strs);
			line = strs[1];
			split(line, ",", strs);
			node.w = std::atoi(strs[0].c_str());
			node.h = std::atoi(strs[1].c_str());
			++count;
			break;
		case 4:
			split(line, ":", strs);
			line = strs[1];
			split(line, ",", strs);
			node.ow = std::atoi(strs[0].c_str());
			node.oh = std::atoi(strs[1].c_str());
			++count;
			break;
		case 5:
			split(line, ":", strs);
			line = strs[1];
			split(line, ",", strs);
			node.ox = std::atoi(strs[0].c_str());
			node.oy = std::atoi(strs[1].c_str());
			++count;
			break;
		case 6:
			split(line, ":", strs);
			node.index = std::atoi(strs[1].c_str());
			count = 0;
			node.oy = node.oh - node.oy - node.h;
			atlas.nodes.push_back(node);
			break;
		}
	}
	return false;
}


void read_atlas(std::ifstream& ifile, vector<Atlas>& atlas) {
	Atlas a;
	string line;
	while (line.empty()) {
		getline(ifile, line);
	}
	while (true) {
		bool ret = read_atlas(ifile, line, a);
		atlas.push_back(a);
		if (!ret) {
			break;
		}
	}
}


bool anti_atlas(const string& path, const Atlas& atlas, bool print) {

	int width = 0, height = 0;
	auto ptr = read_png(path + atlas.png, width, height);
	if (ptr == nullptr) {
		cout << "png read fail" << endl;
		return false;
	}

	if (atlas.format.find("RGBA8888") == string::npos) {
		cout << "format is not RGBA8888" << endl;
		return false;
	}

	for (const auto& node : atlas.nodes) {
		if (node.index != -1) {
			cout << "index is not -1" << endl;
			return false;
		}
	}

	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
		FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);

	auto make_dir = path + atlas.png.substr(0, atlas.png.length() - 4) + "\\";
	CreateDirectory(make_dir.c_str(), NULL);
	for (const auto& node : atlas.nodes) {
		auto arr = clip_png(ptr, width, node);
		save_png(make_dir + node.name + ".png", arr, node.ow, node.oh);
		delete[] arr;
		if (print) {
			cout << node.name << ".";
		}
	}
	delete[] ptr;
	cout << endl;
	return true;
}



bool anti_atlas(const string& path, const string& filename, bool print_nodes) {

	std::ifstream ifile(path + filename);

	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
		FOREGROUND_INTENSITY | FOREGROUND_RED);

	auto file_name = filename.substr(0, filename.length() - 6);
	if (!ifile.is_open()) {
		cout << " atlas open fail" << endl;
		return false;
	}
	vector<Atlas> atlas;
	read_atlas(ifile, atlas);
	ifile.close();

	if (atlas.empty()) {
		cout << " atlas empty" << endl;
		return false;
	}



	if (atlas.size() == 1) {
		cout << " ";
		return anti_atlas(path, atlas[0], print_nodes);
	}

	bool ret = true;
	int i = 0;
	for (const auto& a : atlas) {
		if (++i > 1) {
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
				FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
			cout << a.png.substr(0, a.png.length() - 4) << " ";
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
				FOREGROUND_INTENSITY | FOREGROUND_RED);
		}
		if (!anti_atlas(path, a, print_nodes)) {
			ret = false;
		}
	}
	return ret;
}
